import { StorageKey } from '@/constants/storage';
import Taro from '@tarojs/taro';
import { AppLoadParamsProps } from '@/interfaces/base';
import qs from 'qs';
/**
 * 处理启动参数
 * @param query
 * @returns
 */
export function formatAppLoadParams(query: AppLoadParamsProps) {
  // 处理启动参数
  //  扫描二维码打开，二维码地址在q上
  if (query?.q) {
    const url = decodeURIComponent(query.q);
    // url http://www.a.com?name=zs&age=18;
    const paramStr = url.split('?')?.[1];
    // name=zs&age=18;
    const params = qs.parse(paramStr);
    // 最终结果为：params  {name:'zs',age:18}
    console.log('params', params);
    const { q, ...rest } = query;
    Taro.setStorageSync(StorageKey.APP_LOAD_PARAMS, { ...rest, ...params });
    return { ...rest, ...params };
  } else {
    Taro.setStorageSync(StorageKey.APP_LOAD_PARAMS, query ?? {});
    return query ?? {};
  }
}

export function generateUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
